package com.chuzhiyun.chengguoxy.utlis;

import android.annotation.SuppressLint;
import android.os.Build;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import androidx.annotation.RequiresApi;

public class TimeUtils {

    public static Date Sring2Date(String time) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    public static Date Sring2Date2(String time) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = null;
        try {
            parse = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    public static String getTimeSring() {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String time = simpleDateFormat.format(new Date());
        return time;
    }

    /*获得当前日期*/
    public static String getCurrentTime(){
        Date currentTime = new Date();
        @SuppressLint("SimpleDateFormat") SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString;
        dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * yyyy-MM-dd hh:mm:ss
     *
     * @return
     */
    public static String getTimeAndHorstSring() {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String time = simpleDateFormat.format(new Date());
        return time;
    }

    /*获得当前日期*/
    public static String getNowTime(){
        @SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("Etc/GMT-8"));
        return dateFormat.format(new Date());
    }


    public static int getAge(String birth) {

        Date birthDay = Sring2Date(birth);

        Calendar calendar = Calendar.getInstance();
        int yearNow = calendar.get(Calendar.YEAR);  //当前年份
        int monthNow = calendar.get(Calendar.MONTH);  //当前月份
        int dayOfMonthNow = calendar.get(Calendar.DAY_OF_MONTH); //当前日期
        calendar.setTime(birthDay);
        int yearBirth = calendar.get(Calendar.YEAR);
        int monthBirth = calendar.get(Calendar.MONTH);
        int dayOfMonthBirth = calendar.get(Calendar.DAY_OF_MONTH);
        int age = yearNow - yearBirth;   //计算整岁数
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;//当前日期在生日之前，年龄减一
            } else {
                age--;//当前月份在生日之前，年龄减一
            }
        }
        return age;
    }

    public static String getMonthEnByMonth(int month) {
        String[] monthList = new String[]{"January","February","March","April","May","June","July","August","September",
                "October","November","December"};
        return monthList[month%monthList.length];
    }


    public static Date getEndOfDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    public static Date getStartOfDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    //将带有T的时间字符串转换成yyyy-MM-dd HH:mm:ss
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static String convertDate(String strDate) {
        String str = "";
        strDate = strDate.replace("T", " ");
        str = strDate;
        return str;
    }


    @SuppressLint("SimpleDateFormat")
    public static String strToDateFormat(String date) throws ParseException {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        formatter.setLenient(false);
        Date newDate= formatter.parse(date);
        formatter = new SimpleDateFormat("yyyy-MM-dd");
        assert newDate != null;
        return formatter.format(newDate);
    }

    public static boolean TimeCompare(String dateStr){
        //格式化时间
        @SuppressLint("SimpleDateFormat") SimpleDateFormat format  = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr0 = TimeUtils.getCurrentTime();

        try {
            Date nowTime = format.parse(dateStr0);
            Date chooseTime = format.parse(dateStr);

            assert chooseTime != null;
            assert nowTime != null;
            long ti = chooseTime.getTime()-nowTime.getTime();
            System.out.println(ti);
            if(chooseTime.getTime()>nowTime.getTime())//比较时间大小,dt1小于dt2
            {
                return false;
            }
            else
            {
                return true;
            }
        }catch (ParseException e)
        {
            e.printStackTrace();
        }
        return false;
    }
}
